/**
 * BladeX Commercial License Agreement
 * Copyright (c) 2018-2099, https://bladex.cn. All rights reserved.
 * <p>
 * Use of this software is governed by the Commercial License Agreement
 * obtained after purchasing a license from BladeX.
 * <p>
 * 1. This software is for development use only under a valid license
 * from BladeX.
 * <p>
 * 2. Redistribution of this software's source code to any third party
 * without a commercial license is strictly prohibited.
 * <p>
 * 3. Licensees may copyright their own code but cannot use segments
 * from this software for such purposes. Copyright of this software
 * remains with BladeX.
 * <p>
 * Using this software signifies agreement to this License, and the software
 * must not be used for illegal purposes.
 * <p>
 * THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY. The author is
 * not liable for any claims arising from secondary or illegal development.
 * <p>
 * Author: Chill Zhuang (bladejava@qq.com)
 */
package com.xjrsoft.module.bi.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xjrsoft.common.exception.MyException;
import com.xjrsoft.common.model.result.R;
import com.xjrsoft.module.bi.common.annatation.ApiDecryptAes;
import com.xjrsoft.module.bi.common.constant.LauncherConstant;
import com.xjrsoft.module.bi.common.tool.Condition;
import com.xjrsoft.module.bi.common.tool.Func;
import com.xjrsoft.module.bi.common.tool.Kv;
import com.xjrsoft.module.bi.common.tool.Query;
import com.xjrsoft.module.bi.pojo.entity.DynamicModel;
import com.xjrsoft.module.bi.pojo.entity.VisualDb;
import com.xjrsoft.module.bi.service.IVisualDbService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 数据源模块 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping(LauncherConstant.APPLICATION_VISUAL_NAME + "/db")
@Api(value = "可视化数据源配置模块", description = "可视化数据源配置接口")
public class VisualDbController {

	private final IVisualDbService visualDbService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情")
	public R detail(VisualDb db) {
		VisualDb detail = visualDbService.getOne(Condition.getQueryWrapper(db));
		return R.ok(detail);
	}

	/**
	 * 分页 数据源配置表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页")
	public R list(String name, Query query) {
		IPage<VisualDb> pages = visualDbService.page(Condition.getPage(query), Condition.getQueryWrapper(Kv.create().set("name", name), VisualDb.class));
		return R.ok(pages);
	}

	/**
	 * 新增 数据源配置表
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增")
	public R save(@Valid @RequestBody VisualDb db) {
		db.setUrl(db.getUrl().replace("&amp;", "&"));
		if (visualDbService.dbTest(db)) {
			return R.ok(visualDbService.save(db));
		}
		throw new MyException("系统检测数据库未能连通,请检查配置");
	}

	/**
	 * 修改 数据源配置表
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改")
	public R update(@Valid @RequestBody VisualDb db) {
		db.setUrl(db.getUrl().replace("&amp;", "&"));
		if (visualDbService.dbTest(db)) {
			return R.ok(visualDbService.updateById(db));
		}
		throw new MyException("系统检测数据库未能连通,请检查配置");
	}

	/**
	 * 新增或修改 数据源配置表
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改")
	public R submit(@Valid @RequestBody VisualDb db) {
		db.setUrl(db.getUrl().replace("&amp;", "&"));
		if (visualDbService.dbTest(db)) {
			return R.ok(visualDbService.saveOrUpdate(db));
		}
		throw new MyException("系统检测数据库未能连通,请检查配置");
	}


	/**
	 * 删除 数据源配置表
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除")
	public R remove(@RequestParam String ids) {
		return R.ok(visualDbService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 数据源测试连接
	 */
//	@ApiDecryptAes
	@PostMapping("/db-test")
	@ApiOperationSupport(order = 8)
	@ApiOperation(value = "数据源测试连接")
	public R dbTest(@RequestBody VisualDb db) {
		return R.ok(visualDbService.dbTest(db));
	}

	/**
	 * 数据源列表
	 */
	@GetMapping("/db-list")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "下拉数据源")
	public R dbList() {
		return R.ok(visualDbService.dbList());
	}

	/**
	 * 动态执行SQL
	 */
	@ApiDecryptAes
	@PostMapping("dynamic-query")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "动态执行SQL")
	public R dynamicQuery(@RequestBody DynamicModel model) {
		List<LinkedHashMap<String, Object>> linkedHashMaps = visualDbService.dynamicQuery(model.getId(), model.getSql());
		return R.ok(linkedHashMaps);
	}

}
